package multi_thread;

import tree.Tree;

public class ObjectLockTest {
    public final Object moniter = new Object();

    /*class BinTree{
        BinTree lchild;
        BinTree rchild;

        public BinTree(BinTree lchild,BinTree rchild){
            this.lchild = lchild;
            this.rchild = rchild;
        }
    }*/
    int  k = 0;

    public void x() throws InterruptedException {
        synchronized (moniter){
            System.out.println("result: k1 = " + k);
            moniter.wait();
            System.out.println("result: k2 = " + k);
            k = 1;
            System.out.println("result: k5 = " + k);
        }
    }

    public void y() throws InterruptedException {
        synchronized (moniter){
            System.out.println("result: k3 = " + k);
            moniter.notify();
           Thread.sleep(3000);
            System.out.println("result: k4 = " + k);
        }
    }


    public static void main(String [] atgs){

        ObjectLockTest objectLockTest = new ObjectLockTest();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    objectLockTest.x();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    objectLockTest.y();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
